import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6};
        System.out.println(Arrays.toString(array));
        adjust(array);
        System.out.println(Arrays.toString(array));


        // System.out.println(Arrays.toString(array));
        // bubbleSort(array);
        // System.out.println(Arrays.toString(array));

        // int index = binarySearch(array,1);
        // System.out.println(index);

        // int[] ret = transform(array);
        // System.out.println(Arrays.toString(array));
        // System.out.println(Arrays.toString(ret));
    }


    //调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序
    public static void adjust(int[] array){
        int left = 0;
        int right = array.length - 1;
        while (left < right){
            if(array[left] % 2 == 0 && array[right] % 2 != 0){
                int tmp = array[left];
                array[left] = array[right];
                array[right] = tmp;
            }

            if(array[left] % 2 != 0){
                left++;
            }

            if (array[right] % 2 == 0){
                right--;
            }

        }
    }

    //给定一个整型数组, 实现冒泡排序(升序排序)
    public static void bubbleSort(int[] array){
        boolean flag = false;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j+1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            if(false == flag){
                return;
            }
        }

    }

    //给定一个有序整型数组, 实现二分查找
    public static int binarySearch(int[] array,int key){
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = (left + right) / 2;
            if(array[mid] < key){
                left = mid + 1;
            }else if(array[mid] == key){
                return mid;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }

    //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2
    public static int[] transform(int[] array){
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i] * 2;
        }
        return tmp;
    }

}
